<template>
  <!-- 产品推荐配置 -->
  <div class="product-container">
    <el-table
      :key="itemKey"
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
    >
      <!-- <el-table-column show-overflow-tooltip type="selection" width="55" /> -->
      <el-table-column label="序号" show-overflow-tooltip width="95">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="护肤目标" prop="关键问题" show-overflow-tooltip />

      <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(list, scope.row, scope.$index)">
            编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    /> -->
    <table-edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getPrizeList, doEdit } from '@/api/configration'
  import TableEdit from './components/TableEdit'
  // import { getDeletePrizeConfigProps } from '../utils.js'
  export default {
    name: 'SolutionList',
    components: {
      TableEdit,
    },

    data() {
      return {
        imgShow: true,
        apiData: [],
        list: [],
        listLoading: true,
        itemKey: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      console.log('created...')
      this.fetchData()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(list, row, index) {
        this.$refs['edit'].showEdit(list, row, index)
      },
      handleDelete(row) {
        console.log(row, 'handleDelete...')
        if (row.prizeid) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            // const p = getDeletePrizeConfigProps(row, this.apiData)
            const { error, data } = await doEdit(p)
            this.$baseMessage(data, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids: ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        console.log('handleSizeChange...', this.itemKey)

        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        console.log('handleCurrentChange...', this.itemKey)

        this.queryForm.pageNo = val
        this.fetchData()
      },
      handleQuery() {
        console.log('handleQuery...', this.itemKey)

        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        console.log('fetchData...', this.itemKey)
        this.itemKey = !this.itemKey
        this.apiData = {}
        this.list = []
        this.listLoading = true
        this.$store
          .dispatch('config/getSolutionList')
          .then((data) => {
            console.log('config/getSolutionList', data)
            this.apiData = data

            this.list = data

            this.total = this.list.length
            setTimeout(() => {
              this.listLoading = false
            }, 500)
          })
          .catch()
      },
    },
  }
</script>
